package online.misscl.lsy.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import online.misscl.lsy.controller.BaseController;
import online.misscl.lsy.entity.VaccineUser;
import online.misscl.lsy.entity.dto.LoginDTO;
import online.misscl.lsy.entity.dto.RefineUserInfoDTO;
import online.misscl.lsy.entity.dto.UserRegisterDTO;
import online.misscl.lsy.entity.dto.vo.LoginVaccineUserVo;
import online.misscl.lsy.service.VaccineUserService;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.JwtUtils;
import online.misscl.lsy.util.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author misscl
 * @since 2023/03/11 11:14
 */
@RestController
@RequestMapping("/front/vaccineUser")
@Api(tags = "用户模块")
public class VaccineUserController extends BaseController {

    @Autowired
    private VaccineUserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult userLogin(@RequestBody @Validated LoginDTO loginDTO) {
        LambdaQueryWrapper<VaccineUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VaccineUser::getUsername, loginDTO.getUsername());
        VaccineUser user = userService.getOne(queryWrapper);
        // 验证账号
        if(user == null) {
            return AjaxResult.error("账号不存在！！");
        }
        // 验证密码
        if(!user.getPassword().equals(DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes()))) {
            return AjaxResult.error("密码有误！！");
        }
        return AjaxResult.success("登录成功", JwtUtils.generateToken(user.getUserId(),user.getAuth()));
    }

    @ApiOperation(value = "注册账号")
    @PostMapping("register")
    public AjaxResult register(@RequestBody @Validated UserRegisterDTO userRegisterDTO) {
        LambdaQueryWrapper<VaccineUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VaccineUser::getUsername, userRegisterDTO.getUsername());
        VaccineUser user = userService.getOne(queryWrapper);
        if(user != null) {
            return AjaxResult.error(ResultCode.ACCOUNT_EXISTS, "账号已经存在");
        }
        VaccineUser registerUser = new VaccineUser();
        String password = userRegisterDTO.getPassword();
        // md5加密
        BeanUtils.copyProperties(userRegisterDTO,registerUser);
        registerUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        registerUser.setAuth(0);
        return toAjax(userService.save(registerUser));
    }

    @ApiOperation(value = "完善个人信息")
    @PutMapping("/refineUserInfo")
    public AjaxResult refineUserInfo(@RequestBody @Validated RefineUserInfoDTO refineUserInfoDTO) {
        VaccineUser vaccineUser = userService.getById(getUserId());
        BeanUtils.copyProperties(refineUserInfoDTO,vaccineUser);
        return toAjax(userService.updateById(vaccineUser));
    }

    @ApiOperation(value = "获取登录信息")
    @GetMapping("/getLogin")
    public AjaxResult getLogin() {
        VaccineUser vaccineUser = userService.getById(getUserId());
        LoginVaccineUserVo vaccineUserVo = new LoginVaccineUserVo();
        BeanUtils.copyProperties(vaccineUser,vaccineUserVo);
        return AjaxResult.success(vaccineUserVo);
    }

}
